package cn.raven.serve.service.impl;

import cn.raven.basic.constant.BaseConstant;
import cn.raven.basic.exception.BusinessException;
import cn.raven.basic.utils.BaiduAuditUtils;
import cn.raven.basic.utils.CodeGenerateUtils;
import cn.raven.basic.utils.LoginContext;
import cn.raven.order.domain.OrderProduct;
import cn.raven.order.mapper.OrderProductMapper;
import cn.raven.pet.domain.Pet;
import cn.raven.pet.domain.PetDetail;
import cn.raven.pet.domain.PetOnlineAuditLog;
import cn.raven.serve.domain.Product;
import cn.raven.serve.domain.ProductDetail;
import cn.raven.serve.domain.ProductOnlineAuditLog;
import cn.raven.serve.mapper.ProductDetailMapper;
import cn.raven.serve.mapper.ProductMapper;
import cn.raven.serve.mapper.ProductOnlineAuditLogMapper;
import cn.raven.serve.service.IProductService;
import cn.raven.basic.service.impl.BaseServiceImpl;
import cn.raven.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Raven
 * @since 2022-07-21
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    /**
    * @Title: add
    * @Description: 强化新增
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 9:40
    * @Parameters: [product]
    * @Return void
      */
    @Override
    public void add(Product product) {
        //新增pet基本信息，返回自增id
        productMapper.add(product);
        ProductDetail detail = product.getDetail();
        //给petId赋值
        detail.setProductId(product.getId());
        //保存
        productDetailMapper.add(detail);
    }
    /** 
    * @Title: update
    * @Description: 强化修改
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 9:42  
    * @Parameters: [product]
    * @Return void
      */
    @Override
    public void update(Product product) {
        productMapper.update(product);
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        detail.setOrderNotice(detail.getOrderNotice());
        detail.setIntro(detail.getIntro());
        productDetailMapper.updateByProductId(detail);
    }
    /** 
    * @Title: del
    * @Description: 强化删除
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 9:42  
    * @Parameters: [id]
    * @Return void
      */
    @Override
    public void del(Long id) {
        productMapper.del(id);
        productDetailMapper.delByProductId(id);
    }

    @Override
    public void batchDel(Long[] ids) {
        productMapper.batchDel(ids);
        productDetailMapper.batchDel(ids);
    }
    /**
    * @Title: findByPetId
    * @Description: 服务详情回显
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 10:02
    * @Parameters: [id]
    * @Return cn.raven.pet.domain.PetDetail
      */
    @Override
    public ProductDetail findByPetId(Long id) {
        return productDetailMapper.findByPetId(id);
    }

    /**
    * @Title: onsale
    * @Description: 批量上架服务
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 10:25
    * @Parameters: [ids]
    * @Return void
      */
    @Override
    public void onsale(List<Long> ids) {
        //遍历ids
        List<Long> pIds = new ArrayList<>();
        ProductOnlineAuditLog log = new ProductOnlineAuditLog();
        for (Long id : ids) {
            Product product = productMapper.findOne(id);
            if (product.getState() == 1) {
                continue;
            }
            StringBuffer stringBuffer = new StringBuffer();
            if (!BaiduAuditUtils.TextCensor(product.getName())) {
                stringBuffer.append("服务名称不合规，服务名：" + product.getName());
            }
            String[] split = product.getResources().split(",");
            for (String picture : split) {
                String url = BaseConstant.FastDfsConstant.FASTDFS_PREFIX + picture;
                if (!BaiduAuditUtils.ImgCensor(url)) {
                    stringBuffer.append("服务图片不合规，图片路径：" + url);
                }
            }
            if (stringBuffer.length() > 0) {
                log.setProductId(id);
                log.setState(0);
                log.setNote(stringBuffer.toString());
                productOnlineAuditLogMapper.add(log);
                continue;
            }
            pIds.add(id);
        }
        if (pIds.size() > 0) {
            productMapper.onsale(pIds);
            for (Long id:pIds){
                log.setProductId(id);
                log.setState(1);
                log.setNote("上架成功");
                productOnlineAuditLogMapper.add(log);
            }

        }
    }

    /**
    * @Title: offsale
    * @Description: 批量下架服务
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 10:25
    * @Parameters: [ids]
    * @Return void
      */
    @Override
    public void offsale(List<Long> ids) {
        productMapper.offsale(ids);
        ProductOnlineAuditLog log = new ProductOnlineAuditLog();
        for (Long id:ids){
            log.setProductId(id);
            log.setState(0);
            log.setNote("下架成功");
            productOnlineAuditLogMapper.add(log);
        }
    }

}
